<?php

/**
 * 
 * @author yuxun
 *
 */
class RecourseChannelController extends AdminBaseController {
	
    public function index(){
		$this->setTitle('资源渠道管理');
	
		$filter = new PubFilter();
		$grid   = new PubGrid();
	
		$filter->addFilter('channel', 'input', '渠道');
	
		$params = $filter->get();
		$page = $this->R('page', '1');
		$m = $this->R('m');
		$params['m'] = $m;
		$header = RecourseChannelService::indexHeader();
		if(PubFunc::isAjax()){
			$result = RecourseChannelService::indexData($params);
			$this->out = array(
					'count'     => $result['count'],
					'data'      => $result['data'],
					'result'    => true
			);
			$this->outType = 'json';
		}else{
			$order  = 'create_time';
			$url = PubFunc::ajaxUrl("?r=recourseChannel/index", $params);
			$grid->addGrid($header, $url, array(
					'name'=>'数据',
					'order'=>array($order, 'desc'),
					'type'=>'recourseChannel/index',
					'page'=>$page
			));
	
			$this->out['filter'] = $filter->show();
			$this->out['grid'] = $grid->show();
	
		}
	}
	
	public function add(){
		$this->setTitle('添加资源渠道');
	
		$typeList = RecourseChannelService::typeList();
		$this->out['typeList'] = $typeList;
	}
	
	public function doAdd(){
		$count = RecourseChannelService::repeat(0, 'channel', $this->R('channel'),$this->R('g'));
		if($count > 0){
			$this->showMessage('该渠道已经存在');
		}else{
			$result = RecourseChannelService::doAdd($_POST);
	
			if($result['state'] !== false){
				$this->showMessage($result['msg'], PubFunc::buildUrl("/oss/?r=recourseChannel/index"));
			}else{
				$this->showMessage($result['msg']);
			}
		}	
	}
	
	public function edit(){
		$id = $this->R('id');
		$info = RecourseChannelService::get($id);
		 
		if(empty($info)) $this->showMessage('找不到该信息');
		 
		$this->setTitle('编辑');
		 
		$this->out['info'] = $info;
		 
		$typeList = RecourseChannelService::typeList();
		$this->out['typeList'] = $typeList;
		 
		$backUrl = PubFunc::buildBackUrl("/oss/?r=recourseChannel/index");  //返回路径
		$this->out['backUrl'] = $backUrl;
		 
		$page = $this->R('page');
		$this->out['page'] = $page;
	}
	
	public function doEdit(){
		$id = $this->R('id');
		$count = RecourseChannelService::repeat($id, 'channel', $this->R('channel'),$this->R('g'));
		if($count > 0){
			$this->showMessage('该渠道已经存在');
		}else{
			$result = RecourseChannelService::doEdit($id, $_POST);
			
			if($result !== false){
				$this->showMessage('编辑成功', PubFunc::buildBackUrl("/oss/?r=recourseChannel/index"));
			}else{
				$this->showMessage($result['msg']);
			}
		}
		
		
	}
	

	public function delete(){
		$id = $this->R('id');
	    $result = RecourseChannelService::delete($id);
	    if($result['state']  !== false){
	    	$this->showMessage($result['msg'], PubFunc::buildBackUrl("/oss/?r=recourseChannel/index"));
	    }else{
	    	$this->showMessage($result['msg'], PubFunc::buildBackUrl("/oss/?r=recourseChannel/index"));
	    }	 
	}
	
	


}